/*
 UIJoin 登陆界面
*/
var app = require("ddz_app");
cc.Class({
    extends: require(app.subGameName + "_BaseForm"),

    properties: {
        "cardNode": {
            "default": null,
            "tooltip": "卡牌实例化的底板对象",
            "type": cc.Node,
        },
        "touxiang": {
            "default": null,
            "tooltip": "头像实例化的底板对象",
            "type": cc.Node,
        },
        "layout": {
            "default": null,
            "tooltip": "存放头像实例化节点的面板",
            "type": cc.Node,
        },
    },
    OnCreateInit: function () {
        this.PokerCard = app[app.subGameName + "_PokerCard"]();
        this.GameManager = app[app.subGameName + "_GameManager"]();
        this.HeroManager = app[app.subGameName + "_HeroManager"]();
        this.LogicDDZGame = app.LogicDDZGame();
    },

    OnShow() {
        //每次展示之前先清除旧的组件
        this.HillAll();
        
        //获取之前请求过，存在room的信息
        let RoomMgr = app.DDZRoomMgr();
        let room = RoomMgr.GetEnterRoom();
        let lookInfo = room.getGmLookCard();

        //没有有效的透视消息
        if (!lookInfo) {
            console.warn("透视信息获取失败 lookInfo：" + lookInfo);
            return;
        }
        //储存的对象转为数组
        lookInfo = Object.values(lookInfo)[0];
        
        for (let i = 0; i < lookInfo.length; i++) {
            //剔除自己
            let MyID = this.HeroManager.GetHeroID();
            if (MyID == lookInfo[i].pid) {
                continue;
            }
            //实例化头像
            let tempHead = cc.instantiate(this.touxiang);
            let NameLabel = tempHead.getChildByName("sp_info").getChildByName("lb_name").getComponent(cc.Label);
            NameLabel.string = lookInfo[i].name;
            
            //显示添加
            tempHead.active = true;
            this.layout.addChild(tempHead);

            //排序一下
            this.LogicDDZGame.SortCardByMax(lookInfo[i].list);

            //实例化牌
            for (let j = 0; j < lookInfo[i].list.length; j++) {
                let tempCard = cc.instantiate(this.cardNode);
                let tempValue = lookInfo[i].list[j];
                this.PokerCard.GetPokeCard(tempValue, tempCard);
                //显示添加
                tempCard.active = true;
                let content = tempHead.getChildByName("scroll").getChildByName("view").getChildByName("content");
                content.addChild(tempCard);
            }
        }
    },

    OnClick(btnName, btnNode) {
        switch (btnName) {
            case "btn_close":
                this.CloseForm();
                break;

            default:
                break;
        }
    },

    HillAll(){
        this.layout.removeAllChildren()
        this.touxiang.active = false
        this.cardNode.active = false
    }
});
